﻿using Aurora.Module.Contexts;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Aurora.Module
{
    public abstract class AbpModule : IAbpModule,
        IOnPreApplicationInitialization,
        IOnApplicationInitialization,
        IOnPostApplicationInitialization,
        IOnApplicationShutdown,
        IPreConfigureServices,
        IPostConfigureServices
    {

        protected internal bool SkipAutoServiceRegistration { get; protected set; }

        private ServiceConfigurationContext? _serviceConfigurationContext;
        protected internal ServiceConfigurationContext ServiceConfigurationContext
        {
            get
            {
                if (_serviceConfigurationContext == null)
                {
                    throw new ArgumentException($"{nameof(ServiceConfigurationContext)} is only available in the {nameof(ConfigureServices)},{nameof(PreConfigureServices)} and {nameof(PostConfigureServices)} methods.");
                }
                return _serviceConfigurationContext;
            }
            internal set => _serviceConfigurationContext = value;
        }
        public virtual void PreConfigureServices(ServiceConfigurationContext context)
        {

        }

        public virtual Task PreConfigureServicesAsync(ServiceConfigurationContext context)
        {
            PreConfigureServices(context);
            return Task.CompletedTask;
        }

        public virtual void ConfigureServices(ServiceConfigurationContext context)
        {

        }

        public virtual Task ConfigureServicesAsync(ServiceConfigurationContext context)
        {
            ConfigureServices(context);
            return Task.CompletedTask;
        }

        public virtual void OnApplicationInitialization([NotNull] ApplicationInitializationContext context)
        {

        }

        public virtual Task OnApplicationInitializationAsync([NotNull] ApplicationInitializationContext context)
        {
            OnApplicationInitialization(context);
            return Task.CompletedTask;
        }

        public virtual void OnApplicationShutdown([NotNull] ApplicationShutdownContext context)
        {

        }

        public virtual Task OnApplicationShutdownAsync([NotNull] ApplicationShutdownContext context)
        {
            OnApplicationShutdown(context);
            return Task.CompletedTask;
        }

        public virtual void OnPostApplicationInitialization([NotNull] ApplicationInitializationContext context)
        {

        }

        public virtual Task OnPostApplicationInitializationAsync([NotNull] ApplicationInitializationContext context)
        {
            OnPostApplicationInitialization(context);
            return Task.CompletedTask;
        }

        public virtual void OnPreApplicationInitialization([NotNull] ApplicationInitializationContext context)
        {

        }

        public virtual Task OnPreApplicationInitializationAsync([NotNull] ApplicationInitializationContext context)
        {
            OnPreApplicationInitialization(context);
            return Task.CompletedTask;
        }

        public virtual void PostConfigureServices(ServiceConfigurationContext context)
        {

        }

        public virtual Task PostConfigureServicesAsync(ServiceConfigurationContext context)
        {
            PostConfigureServices(context);
            return Task.CompletedTask;
        }


        /// <summary>
        /// 是否是 abp model 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>

        public static bool IsAbpModule(Type type)
        {
            var typeInfo = type.GetTypeInfo();

            return typeInfo.IsClass &&
                !typeInfo.IsAbstract &&
                !typeInfo.IsGenericType &&

                typeof(IAbpModule).GetTypeInfo().IsAssignableFrom(type);

        }

        internal static void CheckAbpModelType(Type moduleType)
        {
            if (!IsAbpModule(moduleType))
            {
                throw new ArgumentException($"Given type is not an ABP module: " + moduleType.AssemblyQualifiedName);
            }
        }


        /// <summary>
        /// configure
        /// </summary>
        /// <typeparam name="TOptions"></typeparam>
        /// <param name="configureOptions"></param>
        protected void Configure<TOptions>(Action<TOptions> configureOptions) where TOptions : class
        {
            ServiceConfigurationContext.Services.Configure(configureOptions);
        }


        protected void Configure<TOptions>(string name, Action<TOptions> configureOptions) where TOptions : class
        {
            ServiceConfigurationContext.Services.Configure(name, configureOptions);
        }

        protected void PreConfigure<TOptions>(Action<TOptions> configureOptions)
      where TOptions : class
        {
            ServiceConfigurationContext.Services.PreConfigure(configureOptions);
        }

        protected void PostConfigure<TOptions>(Action<TOptions> configureOptions)
            where TOptions : class
        {
            ServiceConfigurationContext.Services.PostConfigure(configureOptions);
        }

        protected void PostConfigureAll<TOptions>(Action<TOptions> configureOptions)
            where TOptions : class
        {
            ServiceConfigurationContext.Services.PostConfigureAll(configureOptions);
        }


    }
}
